home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / runtime / crc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  691 b   |  34 lines

  1. /* CRC function written by Andrew Appel, not tested since last edit. */
  2. #include <stdio.h>
  3.  
  4. #define POLY 0x48000000
  5. /* this stands for the prime polynomial x^32+x^7+x^3, with the high-order
  6.    term removed, and then bits reversed */
  7.  
  8. #define LOG 8  /* any number 1-31 will do */
  9.  
  10. static unsigned long crctab[1<<LOG];
  11.  
  12. static initcrc()
  13. {int i,j, sum;
  14.  for (i=0; i<(1<<LOG); i++)
  15.    {sum=0;
  16.     for(j= LOG-1; j>=0; j=j-1)
  17.      if (i&(1<<j)) sum ^= ((unsigned long)POLY)>>j;
  18.     crctab[i]=sum;
  19.    }
  20. }
  21.  
  22. unsigned long crc(f) FILE *f;
  23. {register int c, h=0;
  24.  while ((c=getc(f))!=EOF)
  25.     h = (h>>LOG)^crctab[(h^c) & ((1<<LOG)-1)];
  26.  return h;
  27. }
  28.  
  29. main()
  30. {
  31.  initcrc();
  32.  printf("%08x\n",crc(stdin));
  33. }
  34.